using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class HL_Admin_Admin_LabelIO : Admin, IRequiresSessionState
{
	protected TextBox TxtLabelMdb;
	protected Button BtnTestDataSource;
	protected Label TxtTestStat;
	protected CheckBox ChkSystemDel;
	protected CheckBox ChkClearOut;
	protected ListBox SourceLabel;
	protected CheckBox ChkOutDel;
	protected ListBox TargetLabel;
	protected Panel LabelList;
	protected Literal label_class_type;
	public OleDbDataAdapter access_other_da;
	public SqlDataAdapter sql_other_da;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.admin.CheckAdminPower(56))
		{
			base.WriteLog("View", Language.GetAdminMsg("Log/Access", "0"), "Label");
			base.WriteRightErrMsg();
		}
		if (base.Request["h_action"] != "")
		{
			string text = base.Request["h_action"];
			if (text != null)
			{
				if (!(text == "import"))
				{
					if (text == "delete")
					{
						this.DeleteLabel();
					}
				}
				else
				{
					if (base.Request["h_type"] == "in" || base.Request["h_type"] == "out")
					{
						this.SaveData();
					}
				}
			}
		}
	}
	protected void SaveData()
	{
		string text = base.Request["h_import_value"];
		if (text != null && text != "" && (Text.CheckNumList(text, ',') || Text.IsNumber(text)))
		{
			this.LabelImport(base.Request["h_type"], text);
		}
		else
		{
			this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('请选择要转换的标签')</script>", false);
		}
	}
	protected void ShowLabelList()
	{
		this.LabelList.Visible = true;
		this.BindParm();
		this.BindAccessData();
	}
	protected void BindParm()
	{
		LabelBll labelBll = new LabelBll();
		DataTable allLabel = labelBll.GetAllLabel();
		this.SourceLabel.DataSource = allLabel;
		this.SourceLabel.DataTextField = "Name";
		this.SourceLabel.DataValueField = "ID";
		this.SourceLabel.DataBind();
	}
	protected void BtnTestDataSource_Click(object sender, EventArgs e)
	{
		if (this.TxtLabelMdb.Text != "")
		{
			string path = base.Server.MapPath(this.TxtLabelMdb.Text);
			if (!File.Exists(path))
			{
				this.TxtTestStat.Text = "<span style=\"color:red\">文件不存在，请检查</span>";
			}
			else
			{
				this.TxtTestStat.Text = "<span style=\"color:blue\">测试正常！</span>";
				this.ShowLabelList();
			}
		}
	}
	protected void BindAccessData()
	{
		string str = base.Server.MapPath(this.TxtLabelMdb.Text);
		string connectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + str;
		string cmdText = "select ID,Name,Type from HL_Label";
		OleDbConnection connection = new OleDbConnection(connectionString);
		OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter();
		DataSet dataSet = new DataSet();
		oleDbDataAdapter.SelectCommand = new OleDbCommand(cmdText, connection);
		oleDbDataAdapter.Fill(dataSet, "MyTable");
		DataTable dataSource = dataSet.Tables[0];
		this.TargetLabel.DataSource = dataSource;
		this.TargetLabel.DataTextField = "Name";
		this.TargetLabel.DataValueField = "ID";
		this.TargetLabel.DataBind();
	}
	protected void LabelImport(string TransferType, string ImportIDArray)
	{
		string str = "";
		try
		{
			str = base.Server.MapPath(this.TxtLabelMdb.Text);
		}
		catch (Exception ex)
		{
			base.Response.Write(ex.Message);
			base.Response.End();
		}
		string connectionString = ConfigurationManager.ConnectionStrings["HowaveConnectionString"].ConnectionString;
		DateTime now = DateTime.Now;
		string cmdText;
		string cmdText2;
		if (TransferType == "out")
		{
			cmdText = "Select * From HL_Label where ID in(" + ImportIDArray + ")";
			cmdText2 = "Select top 1 * From HL_Label where 1=2";
		}
		else
		{
			cmdText2 = "Select * From HL_Label where ID in(" + ImportIDArray + ")";
			cmdText = "Select top 0 * From HL_Label";
		}
		DataSet dataSet = new DataSet();
		DataSet dataSet2 = new DataSet();
		SqlConnection sqlConnection = new SqlConnection(connectionString);
		SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
		sqlDataAdapter.SelectCommand = new SqlCommand(cmdText, sqlConnection);
		SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
		sqlCommandBuilder.QuotePrefix = "[";
		sqlCommandBuilder.QuoteSuffix = "]";
		sqlDataAdapter.Fill(dataSet, "HowaveTable");
		string connectionString2 = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + str;
		DataTable dataTable = dataSet.Tables["HowaveTable"];
		OleDbConnection oleDbConnection = new OleDbConnection(connectionString2);
		this.access_other_da = new OleDbDataAdapter();
		this.access_other_da.SelectCommand = new OleDbCommand(cmdText2, oleDbConnection);
		OleDbCommandBuilder oleDbCommandBuilder = new OleDbCommandBuilder(this.access_other_da);
		oleDbCommandBuilder.QuotePrefix = "[";
		oleDbCommandBuilder.QuoteSuffix = "]";
		this.access_other_da.Fill(dataSet2, "OtherTable");
		DataTable dataTable2 = dataSet2.Tables["OtherTable"];
		int num = 0;
		string text = "";
		if (TransferType == "out")
		{
			if (dataTable.Rows.Count > 0)
			{
				if (this.ChkClearOut.Checked)
				{
					this.ExecSql(oleDbConnection, "delete from HL_Label");
				}
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					string str2 = dataTable.Rows[i]["Name"].ToString();
					if (base.Request["h_recover"] == "1")
					{
						text = text + "delete from hl_label where Name='" + str2 + "'~";
					}
					DataRow dataRow = dataTable2.NewRow();
					for (int j = 1; j < dataTable.Columns.Count; j++)
					{
						dataRow[j] = dataTable.Rows[i][j].ToString();
					}
					dataTable2.Rows.Add(dataRow);
				}
				try
				{
					if (text != "")
					{
						this.ExecSql(oleDbConnection, text);
					}
					num = this.access_other_da.Update(dataSet2, "OtherTable");
					if (this.ChkSystemDel.Checked)
					{
						this.ExecSql(sqlConnection, "delete from HL_Label where ID in (" + ImportIDArray + ")");
					}
				}
				catch (Exception ex)
				{
					base.Response.Write(ex.Message);
					base.Response.End();
				}
			}
		}
		else
		{
			if (dataTable2.Rows.Count > 0)
			{
				string value = DateTime.Now.ToString();
				for (int i = 0; i < dataTable2.Rows.Count; i++)
				{
					if (base.Request["h_recover"] == "1")
					{
						text = text + "delete from hl_label where Name='" + dataTable2.Rows[i]["Name"].ToString() + "';";
					}
					DataRow dataRow2 = dataTable.NewRow();
					for (int j = 1; j < dataTable2.Columns.Count; j++)
					{
						dataRow2[j] = dataTable2.Rows[i][j].ToString();
					}
					dataRow2["updateTime"] = value;
					dataTable.Rows.Add(dataRow2);
				}
				try
				{
					if (text != "")
					{
						this.ExecSql(sqlConnection, text);
					}
					num = sqlDataAdapter.Update(dataSet, "HowaveTable");
					if (this.ChkOutDel.Checked)
					{
						this.ExecSql(oleDbConnection, "delete from HL_Label where ID in (" + ImportIDArray + ")");
					}
				}
				catch (Exception ex)
				{
					base.Response.Write(ex.Message);
					base.Response.End();
				}
			}
		}
		base.Response.Write("<script>alert('数据转换成功，共转换成功" + num + "条数据');location.href='Admin_Lable.aspx';</script>");
	}
	protected void DeleteLabel()
	{
		string text = base.Request["h_import_value"];
		if (text != "" || (text != null && (Text.CheckNumList(text, ',') || Text.IsNumber(text))))
		{
			string sQL = "delete from HL_Label where ID in(" + text + ")";
			bool flag = false;
			string text2 = base.Request["h_type"];
			if (text2 != null)
			{
				if (!(text2 == "in"))
				{
					if (text2 == "out")
					{
						string str = base.Server.MapPath(this.TxtLabelMdb.Text);
						string connectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + str;
						OleDbConnection conn = new OleDbConnection(connectionString);
						flag = this.ExecSql(conn, sQL);
					}
				}
				else
				{
					string connectionString2 = ConfigurationManager.ConnectionStrings["HowaveConnectionString"].ConnectionString;
					SqlConnection conn2 = new SqlConnection(connectionString2);
					flag = this.ExecSql(conn2, sQL);
				}
			}
			if (flag)
			{
				this.ShowLabelList();
				this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('数据删除成功！');</script>", false);
			}
		}
		else
		{
			this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('请选择要删除的标签')</script>", false);
		}
	}
	protected bool ExecSql(OleDbConnection Conn, string SQL)
	{
		bool result = true;
		string[] array = new string[]
		{
			SQL
		};
		if (SQL.Contains("~"))
		{
			array = SQL.Split(new char[]
			{
				'~'
			});
		}
		try
		{
			Conn.Open();
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string text = array2[i];
				if (text != "")
				{
					OleDbCommand oleDbCommand = new OleDbCommand(text, Conn);
					oleDbCommand.ExecuteNonQuery();
				}
			}
		}
		catch (Exception ex)
		{
			base.Response.Write("<script>alert('转换数据出错,原因是:" + ex.Message + "');</script>");
			result = false;
		}
		finally
		{
			Conn.Close();
		}
		return result;
	}
	protected bool ExecSql(SqlConnection Conn, string SQL)
	{
		bool result = true;
		SqlCommand sqlCommand = new SqlCommand(SQL, Conn);
		Conn.Open();
		try
		{
			sqlCommand.ExecuteNonQuery();
		}
		catch (Exception ex)
		{
			ex.WriteErrorLog(new string[0]);
			result = false;
		}
		finally
		{
			Conn.Close();
		}
		return result;
	}
}
